<?php
declare (strict_types = 1);

namespace hangpu8\hpcms\model;

use hangpu8\hpcms\Utils\Data;
use think\Model;
// use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class AuthRule extends Model
{
    // 软删除
    // use SoftDelete;
    // protected $deleteTime = 'delete_time';

    // 自动时间戳
    protected $autoWriteTimestamp = 'datetime';
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';

    /**
     * 获取select组件数据
     *
     * @return array
     */
    public function getRuleOption()
    {
        $data = $this->order('id asc')->field('id,title,path,pid')->select()->toArray();
        $dataTree = Data::channelLevel($data,'','&nbsp;','path','pid');
        $trees = $this->getChildren($dataTree);
        return $trees;
    }

    /**
     * 获取无限级菜单
     *
     * @param array $data
     * @return array
     */
    private function getChildren(array $data)
    {
        $trees = [];
        $i = 0;
        foreach ($data as $value)
        {
            $trees[$i] = [
                'label'         => $value['title'],
                'value'         => $value['id']
            ];
            if ($value['_data'])
            {
                $trees[$i]['children'] = $this->getChildren((array)$value['_data']);
            }
            $i++;
        }
        return $trees;
    }
}
